package com.tanhua.bg.controller;

import com.tanhua.bg.service.MessageService;
import com.tanhua.bg.vo.CommentListQueryParam;
import com.tanhua.bg.vo.MessageVo;
import com.tanhua.bg.vo.MsgPageResult;
import com.tanhua.bg.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("manage/messages")
@Slf4j
public class MessageController {
    @Autowired
    private MessageService messageService;


    //消息拒绝
    @PostMapping("reject")
    public ResponseEntity messageReject(@RequestBody(required = false) String[] ids) {
        try {
            Boolean result = this.messageService.messageReject(ids);
            if (result) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.ok("操作失败");
    }

    //消息通过
    @PostMapping("pass")
    public ResponseEntity messagePass(@RequestBody(required = false) String[] ids) {
        try {
            Boolean result = this.messageService.messagePass(ids);
            if (result) {
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.ok("操作失败");
    }

    //消息置顶
    @PostMapping("{id}/top")
    public ResponseEntity messageTop(@PathVariable String id){
        try {
            Boolean result = this.messageService.messageTop(id);
            if (result){
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.ok("操作失败");
    }

    //消息取消置顶
    @PostMapping("{id}/untop")
    public ResponseEntity messageUnTop(@PathVariable String id){
        try {
            Boolean result = this.messageService.messageUnTop(id);
            if (result){
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.ok("操作失败");
    }
    /**
     * 撤销动态
     * @return
     */
    @PostMapping("revocation")
    public ResponseEntity<String> revocation(@RequestBody String[] publishIds){
        //返回结果标记
        Boolean flag = false;
        String message = null;

        try {
            //调用service的方法
            flag = this.messageService.revocation(publishIds);
            if (flag == true){
                message = "撤销成功";
                return ResponseEntity.ok(message);
            }
            message = "撤销失败";
            return ResponseEntity.status(500).body(message);
        } catch (Exception e) {
            log.error("撤销异常",e);
        }


        return ResponseEntity.status(500).build();
    }


    /**
     * 动态详情
     * @param publishId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<MessageVo> messageInformation (@PathVariable("id") String publishId) {
        try {
            MessageVo messageVo = this.messageService.messageInformation(publishId);
            return ResponseEntity.ok(messageVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 消息翻页
     *
     */
    @GetMapping
    public ResponseEntity<MsgPageResult> queryPage(@RequestParam(value = "page",required = false) Integer page,
                                                   @RequestParam(value = "pagesize", required = false) Integer pagesize,
                                                   @RequestParam(value = "sortProp", defaultValue = "createDate") String sortProp,
                                                   @RequestParam(value = "sortOrder", defaultValue = "descending") String sortOrder,
                                                   @RequestParam(value = "id", required = false) String publishId,
                                                   @RequestParam(value = "sd", required = false) Long startTime,
                                                   @RequestParam(value = "ed", required = false) Long endTime,
                                                   @RequestParam(value = "uid", defaultValue = "1") Long uid,
                                                   @RequestParam(value = "state", defaultValue = "all") String state) {
        try {
            //调用service中的方法
            MsgPageResult msgPageResult = this.messageService.queryPage(page,
                    pagesize,sortProp,sortOrder,publishId,startTime,endTime,state);
            return ResponseEntity.ok(msgPageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }














































































    //评论列表翻页,/manage/messages/comments,get
    @GetMapping("comments")
    public ResponseEntity<PageResult> queryCommentListPage(@RequestHeader ("Authorization")String token,
                                                           CommentListQueryParam param){

        try {
            PageResult pageResult=this.messageService.queryCommentListPage(token,param);
            if (null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }







}